﻿package ${Package}.action.manage;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import ${Package}.bo.${table.TableNameAlias}Bo;
import ${Package}.bo.PermissionBo;
import ${Package}.domain.${table.TableNameAlias};
import ${Package}.security.Authority;
import ${Package}.security.PermissionType;
import ${Package}.utils.ReturnResult;
import ${Package}.utils.Status;

	#foreach($c in $ColumnList)
	#if($c.IsPrimary)
	#set ($primaryKeyAlias=$c.ColumnNameAlias)
	#set ($primaryKey=$c.ColumnName)
	#end
    #end
/*
*author:徐洪昌
*blog:http://cnblogs.com/xusir
*datetime:${Utils.GetDate} ${Utils.GetTime}
*/ 
@Controller
@RequestMapping(value = "/manage/${table.TableNameAlias}")
public class ${table.TableNameAlias}Action{
    Logger logger = Logger.getLogger(${table.TableNameAlias}Action.class);
	private static final String MODULECODE = "${table.TableNameAlias}";
    public ${table.TableNameAlias}Action() {
    }

    /**添加数据*/
	 @Authority(moduleCode = MODULECODE,permissionType = PermissionType.Add)
	@ResponseBody
	@RequestMapping(value = "/Add",method=RequestMethod.POST)
	public String Add(@ModelAttribute(value = "${table.TableNameAlias}") ${table.TableNameAlias} entity) {
		${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
		ReturnResult result = bo.Add(entity);
		return JSONObject.toJSONString(result);
    }
	    /**
    * 删除数据
    * */
	 @Authority(moduleCode = MODULECODE,permissionType = PermissionType.Delete)
	@ResponseBody
	@RequestMapping(value = "/Delete",method=RequestMethod.POST)
	public String Delete(@RequestParam(value = "ids", defaultValue = "") String ids) {
		ReturnResult result = new ReturnResult();
		if (!"".equals(ids)) {
			String[] idArr = ids.split(",");
			${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
			result = bo.Delete(idArr);
		} else {			
			result.setStatus(Status.ERROR);
			result.setMsg("未选择任何项!");
		}
		return JSONObject.toJSONString(result);
	}

	    /**修改数据*/
	@Authority(moduleCode = MODULECODE, permissionType = PermissionType.Edit)
	@ResponseBody
	@RequestMapping(value = "/Edit",method=RequestMethod.POST)
	public String Edit(@ModelAttribute(value = "${table.TableNameAlias}") ${table.TableNameAlias} entity) {
		${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
		ReturnResult result = bo.Edit(entity);
		return JSONObject.toJSONString(result);
	}
    /**数据列表*/
	 @Authority(moduleCode = MODULECODE,permissionType = PermissionType.List)
	@ResponseBody
	@RequestMapping(value = "/List",method=RequestMethod.POST)
	public String List(
			@RequestParam(value = "page", required = true, defaultValue = "1") int pageIndex,
			@RequestParam(value = "rows", required = true, defaultValue = "10") int pageSize,
			@RequestParam(value = "SearchKey", defaultValue = "") String title,
			@RequestParam(value = "state", required = true, defaultValue = "-1") int state) {
		Map<String, Object> map=new HashMap<String, Object>();
		if (state>-1) {
			map.put("status", state);	
		}
		if (!"".equals(title)) {
			map.put("title", title);
		}
		${table.TableNameAlias}Bo bo=new ${table.TableNameAlias}Bo();
		ReturnResult result = bo.List(pageIndex,pageSize,map);
		return JSONObject.toJSONString(result);
    }
	    /**
    * 详细数据信息
    * */
	 @Authority(moduleCode =MODULECODE,permissionType = PermissionType.View)
	@ResponseBody
	@RequestMapping(value = "/View",method=RequestMethod.POST)
	public String View(@RequestParam(value = "${primaryKeyAlias}", defaultValue = "0") int id) {
		${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
		ReturnResult result = bo.View(id);
		return JSONObject.toJSONString(result);
    }
	 /**
    * 更新状态
    * */
	@Authority(moduleCode = MODULECODE, permissionType = PermissionType.On)
	@RequestMapping(value = "/Audit",method=RequestMethod.POST)
	@ResponseBody
	public String Audit(
			@RequestParam(value = "status", defaultValue = "0") int status,
			@RequestParam(value = "ids", defaultValue = "") String ids) {
		ReturnResult result = new ReturnResult();
		if (!"".equals(ids)) {
			String[] idArr = ids.split(",");
			${table.TableNameAlias}Bo bo = new ${table.TableNameAlias}Bo();
			result = bo.Audit(idArr, status);
		} else {	
			result.setStatus(Status.ERROR);
			result.setMsg("未选择任何项!");
		}
		return JSONObject.toJSONString(result);
	}
			/**
	 * 排序
	 * @param id
	 * @param sort
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/Sort/{id}/{sort}",method=RequestMethod.POST)
	@Authority(moduleCode = MODULECODE, permissionType = PermissionType.Sort)
	public String sort(@PathVariable(value="id") Integer id,@PathVariable(value="sort") Integer sort) {
		${table.TableNameAlias}Bo bo=new ${table.TableNameAlias}Bo();
		${table.TableNameAlias} entity=new ${table.TableNameAlias}();
		entity.set${primaryKeyAlias}(id);
		entity.setSort(sort);
		ReturnResult result=bo.Edit(entity);
		return JSONObject.toJSONString(result);
	}
 /**
    * 当前用户操作权限
    * */
	@Authority(moduleCode = MODULECODE, permissionType = PermissionType.None)
	@RequestMapping(value = "/Permission",method=RequestMethod.POST)
	@ResponseBody
	public String Permission() {
		PermissionBo bo = new PermissionBo();
		return bo.getCurrentPermission(MODULECODE);
	}

}
